
package com.invest.utils.date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * populate a date from  String to Date
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2012/8/18
 */
public final class DateParseHelper {

	/** The Constant DEFAULT_DATE_FORMAT. */
	public  static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	
	/** date format without any delimiter */
	public static final String DATE_STRING_SHORT ="yyyyMMdd";
	
	public static final String DATE_STRING_LONG ="yyyyMMddHHmmss";

	/** The Constant DATE_FORMAT2. */
	public static final String DATE_FORMAT2 = "yyyy/MM/dd";

	public static final String DATE_YYYY_MM = "yyyy-MM";
	public static final String DATE_YYYY_MM2 = "yyyy/MM";

	/** The Constant DEFAULT_DATE_FORMAT_FULL. */
	public static final String DEFAULT_DATE_FORMAT_HOU = "yyyy-MM-dd HH";
	public static final String DEFAULT_DATE_FORMAT_MIN = "yyyy-MM-dd HH:mm";
	public static final String DEFAULT_DATE_FORMAT_FULL_SEC = "yyyy-MM-dd HH:mm:ss";
	public static final String DEFAULT_DATE_FORMAT_FULL_MILL = "yyyy-MM-dd HH:mm:ss SSS";
	
	private static final String DATE_SPLIT = "(-)|(/)";
	
	/**
	 * Parses String date to Date date( '/' or '-' as split)
	 * 
	 * @param dateStr string date
	 *            
	 * @return the date
	 */
	public static Date parse(String dateStr) {
		String[] defFormats = new String[] {
				DEFAULT_DATE_FORMAT,DATE_FORMAT2, DEFAULT_DATE_FORMAT_HOU,
				DEFAULT_DATE_FORMAT_MIN,DEFAULT_DATE_FORMAT_FULL_SEC,
				DEFAULT_DATE_FORMAT_FULL_MILL, DATE_YYYY_MM ,DATE_YYYY_MM2,DATE_STRING_LONG};

		return parse(dateStr,DATE_SPLIT,defFormats);
	}

	/**
	 * parse date by format
	 * @param dateStr
	 * @param splits
	 * @param formats
	 * @return
	 */
	private static Date parse(String dateStr,String splits,String[] formats){
		
		if (StringUtils.isBlank(dateStr)) {
			return null;
		}
		int year = 0;
		int month = 0;
		if(StringUtils.isNotBlank(splits)){
			String[] ss = dateStr.split(splits);
			if(null == ss || ss.length <2){
				throw new RuntimeException("日期长度不合法");
			}
			try{
				year = Integer.valueOf(ss[0]);
				month = Integer.valueOf(ss[1]);
			}catch(NumberFormatException e){
				throw new RuntimeException("日期只能包含数字");
			}
		}

		Calendar cal = Calendar.getInstance();
		Date toDate = null;
		try {
			toDate = DateUtils.parseDate(dateStr, formats);
			cal.setTime(toDate);
		}catch (ParseException e) {
			throw new RuntimeException("日期格式不合法");
		}

		int yearRes = cal.get(Calendar.YEAR);
		int monthRes = cal.get(Calendar.MONTH) + 1;

		if (year != yearRes || month != monthRes) {
			throw new RuntimeException("日期不合法 ");
		}
		
		return toDate;
	}
	/**
	 * Parses some special day
	 * pattern yyyyMMdd split is null
	 * @param dateStr  the date str
	 *           
	 * @param pattern the pattern
	 *            
	 * @return the date
	 */
	public static Date parseDate(String dateStr, String pattern,String split) {
		return parse(dateStr,split,new String[] { pattern });
	}
	
	/**
	 * 
	 * @param dateStr
	 * @param patterns
	 * @param split
	 * @return
	 */
	public static Date parseDate(String dateStr, String[] patterns, String split) {
		return parse(dateStr, split, patterns);
	}

	/**
	 * judge is a legal date String
	 * 
	 * @param dateStr the date str
	 *            
	 * @return isDate
	 */
	public static boolean isDateString(String dateStr) {

		boolean isDate = true;
		try {
			parse(dateStr);
		} catch (Exception e) {
			isDate = false;
		}
		return isDate;
	}

	/**
	 * Parses the full date. DEFAULT_DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
	 * 
	 * @param dateStr the date str
	 *            
	 * @return the date
	 */
	public static Date parseFullDate(String dateStr) {

		return parseDate(dateStr, DEFAULT_DATE_FORMAT_FULL_SEC,"-");
	}
	public static Date parseShortDate(String dateStr){
		return parseDate(dateStr, DEFAULT_DATE_FORMAT,"-");
	}
	/**
	 * Time abstract.
	 * 
	 * @param date the date
	 *            
	 * @return the string
	 */
	public static String timeAbstract(Date date) {
		return format(date, "HH:mm");
	}

	/**
	 * Format.
	 * 
	 * @param date  the date
	 *           
	 * @return the string
	 */
	public static String format(Date date) {
		return format(date, "yyyyMMdd_HHmm");
	}

	/**
	 * Format short.
	 * 
	 * @param date the date
	 *            
	 * @return the string
	 */
	public static String formatShort(Date date) {
		if(date == null) return null;
		return format(date, DEFAULT_DATE_FORMAT);
	}

	/**
	 * Format long.
	 * 
	 * @param date the date
	 *            
	 * @return the string
	 */
	public static String formatLong(Date date) {
		return format(date, DEFAULT_DATE_FORMAT_FULL_SEC);
	}

	/**
	 * Format.
	 * 
	 * @param date the date
	 *            
	 * @param pattern the pattern
	 *            
	 * @return the string
	 */
	public static String format(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}


}
